package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.dao.CategoryMapperCustom;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author cg.hu
 * @version 1.0.0
 * @date 2021-09-17 11:00
 * @description
 * @since 1.0.0
 */
@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {

    private static  final Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CategoryMapperCustom categoryMapperCustom;

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-17 11:10
     * @description    添加分类
     **/
    @Override
    public ServerResponse addCategory(String categoryName, Integer parentId) {
        if (parentId == null || StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("添加品类参数错误");
        }
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        // 这个分类是可用的
        category.setStatus(true);
        int rowCount = categoryMapper.insert(category);
        if (rowCount == 0) {
            return ServerResponse.createByErrorMessage("添加失败");
        }
        return ServerResponse.createBySuccessMessage("添加成功");
    }

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-17 14:16
     * @description    修改类别名称
     **/
    @Override
    public ServerResponse updateCategoryName(Integer categoryId, String categoryName) {
        if (categoryId == null || StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("添加品类参数错误");
        }
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);
        int rowCount = categoryMapper.updateByPrimaryKeySelective(category);
        if (rowCount == 0) {
            return ServerResponse.createByErrorMessage("修改失败");
        }
        return ServerResponse.createBySuccessMessage("修改成功");
    }

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-17 14:28
     * @description    查询字节点的 category 信息，并且不递归
     **/
    @Override
    public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId) {
        List<Category> categoryList = categoryMapperCustom.selectCategoryChildrenParentId(categoryId);
        if (CollectionUtils.isEmpty(categoryList)) {
            logger.info("未找到当前分类的子分类");
        }
        return ServerResponse.createBySuccess(categoryList);
    }

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-18 9:05
     * @description    获取子节点下的所有分类信息, 递归查询
     **/
    @Override
    public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId) {
        Set<Category> categorySet = Sets.newHashSet();
        findChildCategory(categorySet, categoryId);
        List<Integer> categoryList = Lists.newArrayList();
        if (null != categoryId) {
            for (Category category : categorySet) {
                categoryList.add(category.getId());
            }
        }
        return ServerResponse.createBySuccess(categoryList);
    }

    // 递归算法，算出子节点
    private Set<Category> findChildCategory(Set<Category> categorySet, Integer categoryId) {
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if (null != category) {
            categorySet.add(category);
        }
        // 查询子节点，递归算法一定要有一个退出的条件
        List<Category> categoryList = categoryMapperCustom.selectCategoryChildrenParentId(categoryId);
        for (Category category1 : categoryList) {
            // 调用自己
            findChildCategory(categorySet, category1.getId());
        }
        return categorySet;
    }
}
